﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace AUV5.Web.Service
{
    public partial class MessageAdd : PageBaseService
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string messageid = Request["messageid"] ?? "";
            string sendder = Request["sender"] ?? "";
            string receiver = Request["receiver"] ?? "";
            string serviceid = Request["serviceid"] ?? "";
            string requestid = Request["requestid"] ?? "";
            string content = Request["content"] ?? "";
            string secretkey = Request["secretkey"] ?? "";
            HttpPostedFile resource = Request.Files["resource"];

            if (!IsValidKey(secretkey, messageid, sendder, receiver, serviceid, requestid, content, resource))
            {
                Response.Write("112");//密钥不正确 
                return;
            }

            if (Request["sender"] != null && Request["receiver"] != null && Request["content"] != null)
            {
                Model.Message mes = BLL.MessageBLL.Instance.GetDetail(messageid);
                if (mes != null)
                {
                    Response.Write("306");//要添加的消息已存在 （防止重复添加）
                    return;
                }
                if (sendder == receiver)
                {
                    Response.Write("301");//不能发送信息给自己
                    return;
                }
                Model.UserInfo send = BLL.UserInfoBLL.Instance.GetDetail(sendder);
                if (send == null)
                {
                    Response.Write("301");//无效的用户
                    return;
                }
                Model.UserInfo receive = BLL.UserInfoBLL.Instance.GetDetail(receiver);
                if (receive == null)
                {
                    Response.Write("301");//无效的用户
                    return;
                }

                if (!string.IsNullOrEmpty(serviceid))
                {
                    Model.ServiceInfo service = BLL.ServiceInfoBLL.Instance.GetDetail(serviceid);
                    if (service == null)
                    {
                        Response.Write("300");//无效的服务
                    }
                }
                if (!string.IsNullOrEmpty(requestid))
                {
                    Model.RequestInfo request = BLL.RequestInfoBLL.Instance.GetDetail(requestid);
                    if (request == null)
                    {
                        Response.Write("307");//无效的需求
                        return;
                    }
                }

                if (string.IsNullOrEmpty(content))
                {
                    Response.Write("308");//内容不能为空
                    return;
                }
                else
                {

                    if (BadWordsFilter.Instance.HasBadWord(content))
                    {
                        Response.Write("309");//内容中含有敏感字符
                        return;
                    }
                }

                //发送信息
                Model.Message message = new Model.Message();
                message.MessageID = messageid;// Guid.NewGuid().ToString();
                message.RefServiceID = serviceid;
                message.RefRequestID = requestid;
                message.Sender = sendder;
                message.Receiver = receiver;
                message.Content = content;
                message.ResourcePath = "";
                message.IsRead = false;

                BLL.MessageBLL.Instance.SendMessage(message);

                Response.Write("100");//发送成功
            }
            else
            {
                Response.Write("111");//参数不正确 
            }
        }

        private bool IsValidKey(string secretkey, string messageid, string sendder, string receiver, string serviceid, string requestid, string content, HttpPostedFile resource)
        {
            if (secretkey_enable)
            {
                if (string.IsNullOrEmpty(secretkey))
                {
                    return false;
                }
                else
                {
                    string tempstr = public_key + current_day + messageid + sendder + serviceid + receiver + requestid + content;
                    if (resource != null)
                    {
                        tempstr += resource.ContentLength;
                    }
                    string finalkey = base.EncryptMD5(tempstr).Substring(6, 22);
                    return secretkey == finalkey;
                }
            }
            else
            {
                return true;
            }
        }
    }
}